/*
 * IO.cpp
 *
 *  Created on: Dec 11, 2013
 *      Author: thanhkm
 */

#include "IO.h"

void IO::writeMat(
		const char *fileName,
		const cv::Mat &dictionary,
		const char *dictionaryName){

	cv::FileStorage fs(fileName, cv::FileStorage::WRITE);
	fs << dictionaryName << dictionary;
	fs.release();
}

void IO::readMat(
		const char *fileName,
		cv::Mat &dictionary,
		const char *dictionaryName){
	dictionary = cv::Mat();
	cv::FileStorage fs(fileName, cv::FileStorage::READ);
	fs[dictionaryName] >> dictionary;
	fs.release();
}

void IO::read_csv(
		const std::string& filename,
		std::vector<cv::Mat>& images,
		std::vector<int>& labels){
	char separator = ';';
	std::ifstream file(filename.c_str(), std::ifstream::in);
	if (!file) {
		std::string error_message = "No valid input file was given, please check the given filename.";
		std::cout << error_message << std::endl;
	}
	std::string line, path, classlabel;
	while (getline(file, line)) {
		std::stringstream liness(line);
		getline(liness, path, separator);
		getline(liness, classlabel);
		if(!path.empty() && !classlabel.empty()) {
			images.push_back(cv::imread(path, CV_LOAD_IMAGE_GRAYSCALE));
			labels.push_back(atoi(classlabel.c_str()));
		}
	}
}


